package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.eveinfo.R;
import com.eveinfo.api.asset.ApiAsset;
import com.eveinfo.display.SheetView;
import com.eveinfo.sql.data.Item;
import com.eveinfo.tools.EveInfoPreferences;
import com.eveinfo.tools.Logging;
import com.eveinfo.tools.MyGestureDetector;
import com.eveinfo.tools.Tools;

public class ShipView extends Activity {

	private static final int FLAG_CARGO = 5;
	private static final int FLAG_DRONE_BAY = 87;

	private final ArrayList<ApiAsset> cargoAssets;
	private final ArrayList<ApiAsset> fittingAssets;
	private final ArrayList<ApiAsset> droneAssets;


	private GestureDetector gestureDetector;
	
	public ShipView() {
		this.cargoAssets = new ArrayList<ApiAsset>();
		this.fittingAssets = new ArrayList<ApiAsset>();
		this.droneAssets = new ArrayList<ApiAsset>();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ship);

		int itemID = getIntent().getIntExtra("itemID", -1);
		int typeID = getIntent().getIntExtra("typeID", -1);

		try {

			final ArrayList<Item> types = SheetView.getItems();
			final int typesSize = types.size();
			Item type = null;
			for (int i = 0; i < typesSize; i++) {
				Item t = types.get(i);
				if (t.getTypeID() == typeID) {
					type = t;
				}
				final HashMap<Integer, ArrayList<ApiAsset>> assets = t.getAssets();
				final Set<Integer> keySet = assets.keySet();
				for (int key : keySet) {
					ArrayList<ApiAsset> apiAssets = assets.get(key);
					final int apiAssetsSize = apiAssets.size();
					for (int j = 0; j < apiAssetsSize; j++) {
						ApiAsset asset = apiAssets.get(j);
						final ApiAsset parent = asset.getParent();
						if (parent == null)
							continue;
						if (parent.getItemID() == itemID) {
							final int flag = asset.getFlag();
							if (flag == FLAG_CARGO) {
								cargoAssets.add(asset);
							} else if (flag == FLAG_DRONE_BAY) {
								droneAssets.add(asset);
							} else {
								fittingAssets.add(asset);
							}
						}
					}
				}
			}

			Collections.sort(fittingAssets, new FittingComparator());

			if (type == null) {
				finish();
				return;
			}

			for (int i = 0; i < typesSize; i++) {

			}
			ImageView picture = (ImageView) findViewById(R.id.IVShip);
			picture.setImageDrawable(Tools.getItemPicture(type, EveInfoPreferences.ASSETS_IMAGE_SIZE_64));

			TextView titleTV = (TextView) findViewById(R.id.TVShip);
			titleTV.setText(type.getTypeName());

			ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.VFShip);


			gestureDetector = new GestureDetector(new MyGestureDetector(this, viewFlipper));

			viewFlipper.addView(getCargoView());
			viewFlipper.addView(getDroneView());
			viewFlipper.addView(getFittingView());

		} catch (Exception e) {
			Logging.getInstance().e(e);
			e.printStackTrace();
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(e.toString());
			builder.setMessage(e.getMessage());
			builder.show();
			finish();
		}

	}

	private View getCargoView() {
		final View view = CreateView("< Fitting", "CargoBay", "DroneBay >");
		ListView listView = (ListView) view.findViewById(R.id.LVShipBottom);
		new CargoAdapter(this, R.layout.itemr, listView, cargoAssets, gestureDetector);
		return view;
	}

	private View getDroneView() {
		final View view = CreateView("< CargoBay", "DroneBay", "Fitting >");
		ListView listView = (ListView) view.findViewById(R.id.LVShipBottom);
		new DroneAdapter(this, R.layout.itemr, listView, droneAssets, gestureDetector);
		return view;
	}

	private View getFittingView() {
		final View view = CreateView("<< DroneBay", "Fitting", "CargoBay >>");
		ListView listView = (ListView) view.findViewById(R.id.LVShipBottom);
		new FittingAdapter(this, R.layout.itemr, listView, fittingAssets, gestureDetector);
		return view;
	}

	private View CreateView(String left, String center, String right) {
		LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = vi.inflate(R.layout.shipbottom, null);
		TextView leftTV = (TextView) view.findViewById(R.id.TVShipBottomLeft);
		leftTV.setText(left);
		TextView centerTV = (TextView) view.findViewById(R.id.TVShipBottomCenter);
		centerTV.setText(center);
		TextView rightTV = (TextView) view.findViewById(R.id.TVShipBottomRight);
		rightTV.setText(right);
		ListView listView = (ListView) view.findViewById(R.id.LVShipBottom);
		listView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return gestureDetector.onTouchEvent(event);
			}
		});
		return view;
	}
}
